package com.example.administrator.smartbutler;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.administrator.smartbutler.fragment.ButlerFragment;
import com.example.administrator.smartbutler.fragment.GirlsFragment;
import com.example.administrator.smartbutler.fragment.UserInfoFragment;
import com.example.administrator.smartbutler.fragment.WechatFragment;
import com.example.administrator.smartbutler.ui.SettingActivity;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TabLayout mTabLayout;

    private ViewPager mViewPager;

    private List<String> mTitle;

    private List<Fragment> mFragment;

    //悬浮窗
    private FloatingActionButton fab_setting;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        //去除阴影
        getSupportActionBar().setElevation(0);

        initDate();

        initView();

//        CrashReport.testJavaCrash();


    }

    //初始化数据
    private void initDate() {
        mTitle = new ArrayList<>();
        mTitle.add(getResources().getString(R.string.smartButler));
        mTitle.add(getResources().getString(R.string.weChatNews));
        mTitle.add(getResources().getString(R.string.girlsImage));
        mTitle.add(getResources().getString(R.string.userInfo));

        mFragment = new ArrayList<>();
        mFragment.add(new ButlerFragment());
        mFragment.add(new WechatFragment());
        mFragment.add(new GirlsFragment());
        mFragment.add(new UserInfoFragment());


    }

    //初始化view
    private void initView() {

        mTabLayout = (TabLayout) findViewById(R.id.mTabLayout);
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);

        fab_setting = (FloatingActionButton) findViewById(R.id.fab_setting);

        fab_setting.setOnClickListener(this);

        fab_setting.setVisibility(View.GONE);

        //预加载
        mViewPager.setOffscreenPageLimit(mFragment.size());

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position==0){
                    fab_setting.setVisibility(View.GONE);

                }else {
                    fab_setting.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //设置适配器
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            //选中的item
            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }

            //返回item的个数
            @Override
            public int getCount() {
                return mFragment.size();
            }

            //设置标题
            @Override
            public CharSequence getPageTitle(int position) {
                return mTitle.get(position);
            }
        });

        //绑定
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.fab_setting:

                startActivity(new Intent(this, SettingActivity.class));
                break;
        }


    }
}
